// olorMenu.cpp : Implementation of ColorMenu
#include "stdafx.h"
#include "ColorMenuAddin.h"
#include "olorMenu.h"
#include "DebugMenu.h"
#include <afxdlgs.h>
/////////////////////////////////////////////////////////////////////////////
// ColorMenu
#include "HookMenuWnd.h"
CList<CSubclassWnd*,CSubclassWnd*> ColorMenu::m_SubWndList;
CODMenu ColorMenu::m_ODMenu(false);

STDMETHODIMP ColorMenu::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IolorMenu
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

HRESULT _stdcall ColorMenu::EndInitialize()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())
	
	tagAddinManager addinManager;
	m_pProjectFramework->get_AddinfoManager(&addinManager);

	DWORD threadID = addinManager.m_dwMainThreadID;

	if(threadID==NULL)
		return S_FALSE;

	BOOL bSucceed = ::EnumThreadWindows(threadID,(WNDENUMPROC)EnumSubingWndProc,0);

	if(!bSucceed)
		return S_FALSE;


	m_ODMenu.m_hInstance = (HINSTANCE)addinManager.m_hInstance;

	long* hinslst;
	SAFEARRAY *psa;
	m_pProjectFramework->GetPlugInInstances(&m_ODMenu.nPulgInCounts,&psa);
	SafeArrayAccessData(psa,(void**)&hinslst);

	m_ODMenu.m_hInstanceList = (HINSTANCE * )new long[m_ODMenu.nPulgInCounts];
	memcpy(m_ODMenu.m_hInstanceList,hinslst,sizeof(long)*m_ODMenu.nPulgInCounts);

	SafeArrayUnaccessData(psa);
	SafeArrayDestroy(psa);

	m_ODMenu.EnumerateAndLoadToolbars();

	return S_OK;	
}

BOOL CALLBACK ColorMenu::EnumSubingWndProc( HWND hwnd,LPARAM lParam )
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	CHookMenuWnd *pSubWnd = new CHookMenuWnd();
	pSubWnd->HookWindow(hwnd);
	m_SubWndList.AddTail(pSubWnd);
	return TRUE;
}

HRESULT _stdcall ColorMenu::Uninitialize( LONG lSessionID )
{
	
	AFX_MANAGE_STATE(AfxGetStaticModuleState())
	
	for(int i=0;i<m_SubWndList.GetCount();i++)
	{
		CSubclassWnd*pSubWnd = m_SubWndList.GetAt(m_SubWndList.FindIndex(i));
		delete pSubWnd;
	}
	return S_OK;	
}

STDMETHODIMP ColorMenu::OnDebugMenu()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
	tagAddinManager addinManager;
	m_pProjectFramework->get_AddinfoManager(&addinManager);
	CWnd *pParent = CWnd::FromHandle((HWND)addinManager.m_hMainWnd);
	CDebugMenu *pdlg = new CDebugMenu();
	pdlg->Create(CDebugMenu::IDD,pParent);
	pdlg->ShowWindow(SW_SHOW);

	return S_OK;
}
